﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem.Dialog
{
    public partial class DialogFlow : Form
    {
        TextBox _retText = null;

        private List<FLOW_GROUP> _listGroup = null;//流程集集合
        private List<FLOW> _listFlow = null;//流程集合
        //private FLOW _selectedFlow = null;//选择的流程
        private FLOW_GROUP _group = null;//选择的流程集集合

        public DialogFlow(TextBox box)
        {
            _retText = box;
            InitializeComponent();
        }

        private void SetListViewFlow(FLOW flow)
        {
            //在ListViewFlow中加入流程信息
            ListViewItem item = new ListViewItem();
            item.Text = flow.ID.ToString();
            item.SubItems.Add(flow.NAME);
            item.SubItems.Add(flow.CONTEXT);
            if (flow.NODE_START != null)
                item.SubItems.Add(flow.NODE_START.ToString());
            else
                item.SubItems.Add("");

            if (flow.NODE_END != null)
                item.SubItems.Add(flow.NODE_END.ToString());
            else
                item.SubItems.Add("");

            item.SubItems.Add(flow.NODE_ALL);
            if (flow.SORT != null)
                item.SubItems.Add(flow.SORT.ToString());
            else
                item.SubItems.Add("");

            if (flow.FLOW_GROUP_ID != null)
                item.SubItems.Add(flow.FLOW_GROUP_ID.ToString());
            else
                item.SubItems.Add("");

            this.listViewFlow.Items.Add(item);
        }

        private void DialogFlow_Load(object sender, EventArgs e)
        {
            FLOW_GROUP group = new FLOW_GROUP();
            var selectGroup = MethodWcf.SelectFlowGroupClient(group);
            if (selectGroup.result)
            {
                _listGroup = selectGroup.value;
                for (int i = 0; i < _listGroup.Count; i++)
                {
                    this.comboBoxFlowGroup.Items.Add(_listGroup[i].NAME);
                }
            }
        }

        private void comboBoxFlowGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获得集合的编码
            if (_listGroup.Count == 0)
            {
                return;
            }
            //获取集合的名称
            string groupName = comboBoxFlowGroup.SelectedItem.ToString();

            //清空流程
            this.listViewFlow.Items.Clear();

            //MessageBox.Show(groupName);
            //根据选择的流程集合的名称获得流程的ID
            _group = (from g in _listGroup
                      where g.NAME == groupName
                      select g).FirstOrDefault();

            if (_group == null)
            {
                MessageBox.Show("未能获得流程集合的ID");
                return;
            }

            //根据groupID获得相关的流程表
            FLOW flow = new FLOW();
            flow.SetWhere();
            flow.FLOW_GROUP_ID = _group.ID;

            var selectFlow = MethodWcf.SelectFlowClient(flow);
            if (selectFlow.result)
            {
                _listFlow = (from f in selectFlow.value
                             orderby f.SORT
                             select f).ToList();

                for (int i = 0; i < _listFlow.Count; i++)
                {
                    SetListViewFlow(_listFlow[i]);
                }
            }
        }

        private void listViewFlow_MouseClick(object sender, MouseEventArgs e)
        {
            var choiceId = this.listViewFlow.SelectedItems[0].Text;//选择的ID
            _retText.Text = choiceId;
            this.Close();
        }
    }
}
